package com.zhangxin.aiInterview.manager.redis.audio_emotion;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 音频情感数据获取服务
 */
@Component
public class GetAudioEmotion {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 获取用户的所有音频情感数据
     * @param userId 用户ID
     * @return 情感数据列表
     */
    public List<String> getAllEmotions(Long userId) {
        try {
            String key = getKey(userId);
            Long size = stringRedisTemplate.opsForList().size(key);
            if (size != null && size > 0) {
                return stringRedisTemplate.opsForList().range(key, 0, -1);
            }
        } catch (Exception e) {
            System.out.println("Redis获取音频情感数据失败: " + e.getMessage());
        }
        return new ArrayList<>();
    }

    /**
     * 获取指定范围的音频情感数据
     * @param userId 用户ID
     * @param start 起始索引
     * @param end 结束索引
     * @return 情感数据列表
     */
    public List<String> getEmotionsRange(Long userId, long start, long end) {
        try {
            String key = getKey(userId);
            return stringRedisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            System.out.println("Redis获取音频情感数据范围失败: " + e.getMessage());
            return new ArrayList<>();
        }
    }

    private String getKey(Long userId) {
        return "AI_interview:audioEmotion:" + userId;
    }
}